package ru.myrtle.umf.impl;

import java.util.HashMap;
import java.util.Map;

import ru.myrtle.umf.UClass;
import ru.myrtle.umf.UFeature;
import ru.myrtle.umf.UModel;
import ru.myrtle.umf.UObject;
import ru.myrtle.umf.UReference;
import ru.myrtle.umf.util.UList;
import ru.myrtle.umf.util.UObjectList;

/**
 *
 */
public class UObjectImpl implements UObject {

    private static class Store {

        private final Map<UFeature, Object> properties =
                new HashMap<UFeature, Object>();

        public Object get(UFeature feature) {
            return properties.get(feature);
        }

        public void set(UFeature feature, Object value) {
            properties.put(feature, value);
        }
    }

    /*
     *
     */

    private UObject container;
    private UReference containerReference;

    private Store store;

    /*
     *
     */

    public UObjectImpl() {
    }

    /*
     *
     */

    private Store getStore() {
        if (store == null) {
            store = new Store();
        }

        return store;
    }

    /*
     *
     */

    @Override
    public UClass uClass() {
        return UModel.UOBJECT;
    }

    @Override
    public UObject uContainer() {
        return container;
    }

    @Override
    public UReference uContainerReference() {
        return containerReference;
    }

    @Override
    @SuppressWarnings(
            "unchecked"
    )
    public UList<UObject> uContents() {
        UObjectList<UObject> contents = new UObjectList<UObject>();

        for (UReference reference : uClass().getAllReferences()) {
            if (reference.isContainment()) {
                Object value = uGet(reference);

                if (value != null) {
                    if (reference.isMany()) {
                        contents.addAll(
                                (UList<? extends UObject>) value
                        );
                    }
                    else {
                        contents.add(
                                (UObject) value
                        );
                    }
                }
            }
        }

        return contents;
    }

    @Override
    public Object uGet(UFeature feature) {
        return getStore().get(feature);
    }

    @Override
    public boolean uIsSet(UFeature feature) {
        return false; // todo
    }

    @Override
    public void uSet(UFeature feature, Object value) {
        getStore().set(feature, value);
    }

    @Override
    public void uUnset(UFeature feature) {
        // todo
    }

    @Override
    public void uInternalAttach(UObject container, UReference containerReference) {
        this.container = container;
        this.containerReference = containerReference;
    }

    @Override
    public void uInternalDetach() {
        this.container = null;
        this.containerReference = null;
    }

    @Override
    public void uInternalInverseAdd(UObject otherEnd, UReference reference) {
        // todo
    }

    @Override
    public void uInternalInverseRemove(UObject otherEnd, UReference reference) {
        // todo
    }
}
